package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.bean.Person;
import com.example.demo.bean.QueryInfo;
import com.example.demo.bean.User;
import com.example.demo.dao.PersonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


@RestController
public class PersonController {

    @Autowired
    PersonDao personDao;

    @RequestMapping("/allDoctor")
    public String getDoctorList(QueryInfo queryInfo){

        //模糊查询所有信息
        int  numbers =  personDao.getDoctorCounts("%"+queryInfo.getQuery()+"%","%"+queryInfo.getQuery1()+"%");
        //开始页面
        int pageStart = (queryInfo.getPageStart()-1)*queryInfo.getPageSize();

        List<Person> persons = personDao.getAllDoctor("%"+queryInfo.getQuery()+"%","%"+queryInfo.getQuery1()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String,Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",persons);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/deleteDoctor")
    public String deleteUser(int id){
        System.out.println(id);
        int i = personDao.deleteDoctor(id);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/addDoctor")
    public String addDoctor(@RequestBody Person person){
        System.out.println(person);
        person.setSoleType("1");
        int i = personDao.addDoctor(person);
        String str = i >0?"success":"error";
        return str;
    }

    //获取当前用户信息
    @RequestMapping("/getUpDoctorDate")
    public String getUpDoctorDate(int id){
        System.out.println("编号:"+id);
        Person updateDoctor = personDao.getUpdateDoctor(id);
        String Persons_json = JSON.toJSONString(updateDoctor);
        return Persons_json;
    }

    //修改信息
    @RequestMapping("/editDoctor")
    public String editDoctor(@RequestBody Person person){
        System.out.println(person);
        int i = personDao.editDoctor(person);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/allNurse")
    public String getNurseList(QueryInfo queryInfo){
        //模糊查询所有信息
        int  numbers =  personDao.getNurseCounts("%"+queryInfo.getQuery()+"%");
        //开始页面
        int pageStart = (queryInfo.getPageStart()-1)*queryInfo.getPageSize();
        List<Person> persons = personDao.getAllNurse("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String,Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",persons);
        System.out.println(numbers);
        System.out.println(persons);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/deleteNurse")
    public String deleteNurse(int id){
        System.out.println(id);
        int i = personDao.deleteNurse(id);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/addNurse")
    public String addNurse(@RequestBody Person person){
        System.out.println(person);
        person.setSoleType("2");
        int i = personDao.addNurse(person);
        String str = i >0?"success":"error";
        return str;
    }

    //获取当前用户信息
    @RequestMapping("/getUpNurseDate")
    public String getUpNurserDate(int id){
        System.out.println("编号:"+id);
        Person updateNurse = personDao.getUpdateNurse(id);
        String Persons_json = JSON.toJSONString(updateNurse);
        return Persons_json;
    }

    //修改信息
    @RequestMapping("/editNurse")
    public String editNurse(@RequestBody Person person){
        System.out.println(person);
        int i = personDao.editNurse(person);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/allPatient")
    public String getPatientList(QueryInfo queryInfo){
        //模糊查询所有信息
        System.out.println(queryInfo);
        int  numbers =  personDao.getPatientCounts("%"+queryInfo.getQuery()+"%","%"+queryInfo.getQuery1()+"%");
        //开始页面
        int pageStart = (queryInfo.getPageStart()-1)*queryInfo.getPageSize();
        List<Person> persons = personDao.getAllPatient("%"+queryInfo.getQuery()+"%","%"+queryInfo.getQuery1()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String,Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",persons);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/deletePatient")
    public String deletePatient(int id){
        System.out.println(id);
        int i = personDao.deletePatient(id);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/upDatePatientStage")
    public String upDatePatientStage(@RequestParam("id") Integer  id,
                                     @RequestParam("state") String stage){
        int i = personDao.upDatePatientStage(id, stage);
        System.out.println("用户编号:"+id);
        System.out.println("用户状态:"+stage);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/addPatient")
    public String addPatient(@RequestBody Person person){
        System.out.println(person);
        person.setSoleType("3");
        person.setStage("已预约");
        int i = personDao.addPatient(person);
        String str = i >0?"success":"error";
        return str;
    }

    //获取当前用户信息
    @RequestMapping("/getUpPatientDate")
    public String getUpPatientDate(int id){
        System.out.println("编号:"+id);
        Person updatePatient = personDao.getUpdatePatient(id);
        String Persons_json = JSON.toJSONString(updatePatient);
        return Persons_json;
    }

    //修改信息
    @RequestMapping("/editPatient")
    public String editPatient(@RequestBody Person person){
        System.out.println(person);
        int i = personDao.editPatient(person);
        String str = i >0?"success":"error";
        return str;
    }
}
